package com.example.master.service.Impl;


import com.example.master.dto.*;
import com.example.master.entity.*;
import com.example.master.entity.vo.*;
import com.example.master.mapper.*;
import com.example.master.service.*;
import com.example.master.utils.*;

import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.*;


/**
 * @Author zcb
 * @PackageName a-great-masters
 * @Package com.example.master.service.impl
 * @Date 2023/2/6 14:53
 * @Version 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderUtils orderUtils;


    @Override
    public Result getServiceInfo(Integer serviceId) {


        //根据服务ID查询到卖家ID
        Integer sellerId = orderMapper.sellerId(serviceId);
        //根据卖家ID得到卖家名称
        String sellerNickName = orderMapper.sellerNickName(sellerId);
        //根据服务ID查询到计费方式
        Integer payType = orderMapper.payType(serviceId);
        //根据服务ID查询到价格
        Long price = orderMapper.price(serviceId);

        PurchaseVO purchaseVO = new PurchaseVO(sellerNickName, payType, price);

        return Result.success(purchaseVO);
    }

    @Override
    @Transactional
    public Result getOrderInfo(Integer serviceId, Integer userId, String remark) {
        Order order=new Order();
        OrderParticulars orderParticulars=new OrderParticulars();
        Integer type=1;
        Integer status=0;

        String orderNo= orderUtils.getOrderCode(userId);
        order.setOrderNo(orderNo);
        order.setPrice(orderMapper.price(serviceId));
        order.setType(type);
        order.setStatus(status);
        order.setCreateTime(System.currentTimeMillis());
        order.setCreateBy(userId);
        order.setModifyTime(System.currentTimeMillis());
        order.setModifyBy(userId);
        order.setServiceId(serviceId);
        order.setSellerId(orderMapper.sellerId(serviceId));

        orderParticulars.setOrderNo(orderNo);
        orderParticulars.setServiceImg(orderMapper.img(serviceId));
        orderParticulars.setGameName(orderMapper.getGameName(serviceId));
        orderParticulars.setPayType(type);
        orderParticulars.setPrice(orderMapper.price(serviceId));
        orderParticulars.setPriceRemark(orderMapper.priceRemark(serviceId));
        orderParticulars.setRemark(remark);
        orderParticulars.setCreateTime(System.currentTimeMillis());

        orderMapper.insertOrder(order);
        orderMapper.insertOrderParticulars(orderParticulars);
        return Result.success(orderNo);
    }

    @Override
    public OrderDTO getServiceByOrderNo(String orderNo) {
        OrderDTO orderDTO  = orderMapper.getServiceByOrderNo(orderNo);
        return orderDTO;
    }


}
